package com.hsy.linemoney.apiservice;

import com.deparse.dpframe.tools.utils.DPGsonUtil;
import com.hsy.linemoney.R;
import com.hsy.linemoney.requests.RequestTask;
import com.hsy.linemoney.utils.ToastUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * 我的交易相关
 * Created by MartinKent on 2016/11/28.
 */

public class MyDealService extends BasicService<JSONArray> {

//    获取交付状态与记录
    public void getCompletedOrders(final Callback<CompletedOrdersResult> callback, boolean... clearData) {
        if (null != clearData && clearData.length > 0 && clearData[0]) {
            DataHolder.mCompletedOrdersResult = null;
        }
        if (null != DataHolder.mCompletedOrdersResult) {
            if (null != callback) {
                callback.onSuccess(0, DataHolder.mCompletedOrdersResult);
            }
            return;
        }
        String api = "/LmBusinessServices-portlet.order/user/completedOrders";
        JSONObject params = ParamsMaker.newInstance()
                .add("userId", LineMoneyContext.getLoggedUser().getUserId())
                .get();
        invoke(0, api, params, new Callback<JSONArray>() {
            @Override
            public void onSuccess(int what, JSONArray result) {

                try {
                    DataHolder.mCompletedOrdersResult = DPGsonUtil.fromJson(result.get(0).toString(), CompletedOrdersResult.class);
                    if (null != callback) {
                        callback.onSuccess(0, DataHolder.mCompletedOrdersResult);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    callback.onError(what, 0, e.getLocalizedMessage(), e);
                }
            }

            @Override
            public void onError(int what, int type, String msg, Exception e) {
                if (e instanceof RequestTask.StatusCodeException) {
                    ToastUtil.showToast(msg);
                } else {
                    ToastUtil.showToast(R.string.txt_network_failure);
                }
                if (null != callback) {
                    callback.onError(what, 0, msg, e);
                }
            }
        });
    }

    public static class CompletedOrdersResult {
        private Status status;

        private Data data;

        public void setStatus(Status status) {
            this.status = status;
        }

        public Status getStatus() {
            return this.status;
        }

        public void setData(Data data) {
            this.data = data;
        }

        public Data getData() {
            return this.data;
        }

        public class Data {
            private List<CompletedOrders> completedOrders;

            public void setCompletedOrders(List<CompletedOrders> completedOrders) {
                this.completedOrders = completedOrders;
            }

            public List<CompletedOrders> getCompletedOrders() {
                return this.completedOrders;
            }

        }

        public class CompletedOrders {
            private String postingDate;

            private String estimatedAmount;

            private double fxRate;

            private double fromAmount;

            private String toCurrencyCd;

            private String fromCurrencyCd;

            private String transactionDate;

            public void setPostingDate(String postingDate) {
                this.postingDate = postingDate;
            }

            public String getPostingDate() {
                return this.postingDate;
            }

            public void setEstimatedAmount(String estimatedAmount) {
                this.estimatedAmount = estimatedAmount;
            }

            public String getEstimatedAmount() {
                return this.estimatedAmount;
            }

            public void setFxRate(double fxRate) {
                this.fxRate = fxRate;
            }

            public double getFxRate() {
                return this.fxRate;
            }

            public void setFromAmount(double fromAmount) {
                this.fromAmount = fromAmount;
            }

            public double getFromAmount() {
                return this.fromAmount;
            }

            public void setToCurrencyCd(String toCurrencyCd) {
                this.toCurrencyCd = toCurrencyCd;
            }

            public String getToCurrencyCd() {
                return this.toCurrencyCd;
            }

            public void setFromCurrencyCd(String fromCurrencyCd) {
                this.fromCurrencyCd = fromCurrencyCd;
            }

            public String getFromCurrencyCd() {
                return this.fromCurrencyCd;
            }

            public void setTransactionDate(String transactionDate) {
                this.transactionDate = transactionDate;
            }

            public String getTransactionDate() {
                return this.transactionDate;
            }

        }

        public class Status {
            private int statusCode;

            public void setStatusCode(int statusCode) {
                this.statusCode = statusCode;
            }

            public int getStatusCode() {
                return this.statusCode;
            }

        }
    }

//    我的交易 - 部分完成 - 会删除
    public void getPartDealedOrder(final Callback<PartDealedOrderResult> callback, boolean... clearData) {
        if (null != clearData && clearData.length > 0 && clearData[0]) {
            DataHolder.mPartDealedOrderResult = null;
        }
        if (null != DataHolder.mPartDealedOrderResult) {
            if (null != callback) {
                callback.onSuccess(0, DataHolder.mPartDealedOrderResult);
            }
            return;
        }
        String api = "/LmBusinessServices-portlet.order/user/partialOrders";
        JSONObject params = ParamsMaker.newInstance()
                .add("userId", LineMoneyContext.getLoggedUser().getUserId())
                .get();
        invoke(0, api, params, new Callback<JSONArray>() {
            @Override
            public void onSuccess(int what, JSONArray result) {

                try {
                    DataHolder.mPartDealedOrderResult = DPGsonUtil.fromJson(result.get(0).toString(), PartDealedOrderResult.class);
                    if (null != callback) {
                        callback.onSuccess(0, DataHolder.mPartDealedOrderResult);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    callback.onError(what, 0, e.getLocalizedMessage(), e);
                }
            }

            @Override
            public void onError(int what, int type, String msg, Exception e) {
                if (e instanceof RequestTask.StatusCodeException) {
                    ToastUtil.showToast(msg);
                } else {
                    ToastUtil.showToast(R.string.txt_network_failure);
                }
                if (null != callback) {
                    callback.onError(what, 0, msg, e);
                }
            }
        });
    }

    public static class PartDealedOrderResult {
        //TODO Java Bean
    }

//    我的交易 - 等待交易
    public void getNewOrders(final Callback<NewOrderResult> callback, boolean... clearData) {
        if (null != clearData && clearData.length > 0 && clearData[0]) {
            DataHolder.mNewOrderResult = null;
        }
        if (null != DataHolder.mNewOrderResult) {
            if (null != callback) {
                callback.onSuccess(0, DataHolder.mNewOrderResult);
            }
            return;
        }
//        String api = "/LmBusinessServices-portlet.order/user/newOrders";
        String api = "/UserProfile-portlet.order/user/newOrders";
        JSONObject params = ParamsMaker.newInstance()
                .add("userId", LineMoneyContext.getLoggedUser().getUserId())
//                .add("userId", 133285)
                .get();

        invoke(0, api, params, new Callback<JSONArray>() {
            @Override
            public void onSuccess(int what, JSONArray result) {

                try {
                    DataHolder.mNewOrderResult = DPGsonUtil.fromJson(result.get(0).toString(), NewOrderResult.class);
                    if (null != callback) {
                        callback.onSuccess(0, DataHolder.mNewOrderResult);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    callback.onError(what, 0, e.getLocalizedMessage(), e);
                }
            }

            @Override
            public void onError(int what, int type, String msg, Exception e) {
                if (e instanceof RequestTask.StatusCodeException) {
                    ToastUtil.showToast(msg);
                } else {
                    ToastUtil.showToast(R.string.txt_network_failure);
                }
                if (null != callback) {
                    callback.onError(what, 0, msg, e);
                }
            }
        });
    }

    public static class NewOrderResult {
        private Status status;

        private Data data;

        public void setStatus(Status status) {
            this.status = status;
        }

        public Status getStatus() {
            return this.status;
        }

        public void setData(Data data) {
            this.data = data;
        }

        public Data getData() {
            return this.data;
        }

        public class Data {
            private List<NewOrders> newOrders;

            public void setNewOrders(List<NewOrders> newOrders) {
                this.newOrders = newOrders;
            }

            public List<NewOrders> getNewOrders() {
                return this.newOrders;
            }

        }

        public class NewOrders {
            private String postingDate;

            private String estimatedAmount;

            private double fxRate;

            private double fromAmount;

            private String toCurrencyCd;

            private String fromCurrencyCd;

            private String transactionDate;

            public void setPostingDate(String postingDate) {
                this.postingDate = postingDate;
            }

            public String getPostingDate() {
                return this.postingDate;
            }

            public void setEstimatedAmount(String estimatedAmount) {
                this.estimatedAmount = estimatedAmount;
            }

            public String getEstimatedAmount() {
                return this.estimatedAmount;
            }

            public void setFxRate(double fxRate) {
                this.fxRate = fxRate;
            }

            public double getFxRate() {
                return this.fxRate;
            }

            public void setFromAmount(double fromAmount) {
                this.fromAmount = fromAmount;
            }

            public double getFromAmount() {
                return this.fromAmount;
            }

            public void setToCurrencyCd(String toCurrencyCd) {
                this.toCurrencyCd = toCurrencyCd;
            }

            public String getToCurrencyCd() {
                return this.toCurrencyCd;
            }

            public void setFromCurrencyCd(String fromCurrencyCd) {
                this.fromCurrencyCd = fromCurrencyCd;
            }

            public String getFromCurrencyCd() {
                return this.fromCurrencyCd;
            }

            public void setTransactionDate(String transactionDate) {
                this.transactionDate = transactionDate;
            }

            public String getTransactionDate() {
                return this.transactionDate;
            }

        }

        public class Status {
            private int statusCode;

            public void setStatusCode(int statusCode) {
                this.statusCode = statusCode;
            }

            public int getStatusCode() {
                return this.statusCode;
            }

        }
    }
}
